<template>
  <div class="home-container">
    <!-- 导航栏-顶部搜索栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
    通过animated属性可以开启切换标签内容时的转场动画
    通过swipeable属性可以开启滑动切换标签页 -->

    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <!-- 频道tab -->
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <!-- 使用子组件 频道的文章列表 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isEditChannelShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      class="edit-channel-popup"
      v-model="isEditChannelShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-right"
    >
      <!-- 传递channels 传递active高亮索引 定义update-active事件-->
      <channel-edit
        @update-active="onUpdateActive"
        :myActive="active"
        :myChannels="channels"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'// 导入子组件-文章列表
import ChannelEdit from './components/channel-edit'// 导入子组件-频道编辑
import { getItem } from '@/utils/storage.js'// 导入从本地读取数据的方法 数据操作api
export default {
  name: 'HomeIndex',
  // 注册子组件
  components: { ArticleList, ChannelEdit },
  props: {},
  data () {
    return {
      active: 0,
      // 定义数据接收频道列表
      channels: [],
      isEditChannelShow: false // 控制弹窗的显示与否看到弹窗的页面了

    }
  },
  // 组件钩子函数 可以访问到data数据
  created () {
    if (this.$store.state.user) {
      // 已登录  去服务器调用获取频道列表
      this.loadChannels()
    } else {
      // 未登录 从本地读取数据
      this.channels = getItem('TOUTIAO_CHANNELS')
    }
  },
  methods: {
    // 2. 定义加载频道列表数据的方法
    async loadChannels () {
      try {
        const { data } = await getUserChannels()
        this.channels = data.data.channels
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },

    onUpdateActive (index, isEditChannelShow = true) {
      // 更新激活的频道项
      this.active = index
      // 关闭弹出层
      this.isEditChannelShow = isEditChannelShow
    }
  }
}

</script>

<style scoped lang="less">
.home-container {
  padding-top: 180px;
  padding-bottom: 100px;
  // 1.标题栏---------------------------------
  /deep/.van-nav-bar__title {
    //  /deep/ 深度选择器 父组件中的样式可以影响子组件
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #c2c2c2;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }

  // 2.tab栏 ---------------------------------
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }

    .van-tab--active .van-tab__text {
      color: #333 !important;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
  }

  // 汉堡按钮---------------------------------
  // 汉堡按钮的占位符
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  // 3.2 汉堡按钮样式
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 2px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
  // 弹出层
  .edit-channel-popup {
    padding-top: 100px;
    box-sizing: border-box;
  }
}
</style>
